/*
 * Created by JFormDesigner on Sat Dec 26 10:44:49 CST 2020
 */

package cn.fourhundredandeighteen.view.AdminView.AllcourseFind;

import java.beans.*;
import cn.fourhundredandeighteen.controller.CourseController;
import cn.fourhundredandeighteen.controller.TeacherController;
import cn.fourhundredandeighteen.entity.Course;
import cn.fourhundredandeighteen.entity.Teacher;
import cn.fourhundredandeighteen.utill.Result;
import cn.fourhundredandeighteen.view.AdminView.AllcourseFind.AddCourse.addCourse.addCourse;
import cn.fourhundredandeighteen.view.AdminView.AllcourseFind.UpDate.upData;
import cn.fourhundredandeighteen.view.Component.Button;
import cn.fourhundredandeighteen.view.Component.Container;

import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;

/**
 * @author Br.S
 */
public class allCoursefind extends JPanel {
    CourseController courseController = new CourseController();
    Result<List<Course>> course = courseController.getCourse();

    private JScrollPane jScrollPane = viewCourseTable(course.getData());
    public allCoursefind() {
        initComponents();
        this.add(jScrollPane);
        this.updateUI();

    }
    private void getData(){

        this.remove(jScrollPane);
        jScrollPane = viewCourseTable(course.getData());
        this.add(jScrollPane);
        this.updateUI();
    }
    private void button1MouseClicked(MouseEvent e) {
        // TODO add your code here
        try {
            Result result = courseController.delCourse(Long.valueOf(textField1.getText()));
            if (result.getMassage()=="SUCCESS"){
                JOptionPane.showMessageDialog(null,"删除成功");
              //  getData();
                Container.getInstance().upDatePage();
            }
        }catch (NumberFormatException numberFormatException){
            JOptionPane.showMessageDialog(null,"请先选择课程");
        }
    }
    private void button1ActionPerformed(ActionEvent e) {
        // TODO add your code here.
    }
    private  JScrollPane jScrollPane(Object[][] tableData,String[] name){
        JTable table=new JTable(tableData,name);
        table.setSize(1000,800);
        table.setBackground(Color.WHITE);
        table.setRowHeight(32);//设置行高
        DefaultTableCellRenderer r = new DefaultTableCellRenderer();
        r.setHorizontalAlignment(JLabel.CENTER);
        table.setDefaultRenderer(Object.class, r);
        // 设置表格内容颜色
        table.getColumnModel().getColumn(6).setPreferredWidth(100);
        table.getColumnModel().getColumn(7).setPreferredWidth(100);
        table.setForeground(new Color(47,53,66));                   // 字体颜色
        table.setFont(new Font(null, Font.PLAIN, 15));      // 字体样式
        table.setSelectionForeground(new Color(236,240,241));      // 选中后字体颜色
        table.setSelectionBackground(new Color(87,96,111));     // 选中后字体背景
        table.setGridColor(new Color(87,96,111));                     // 网格颜色
        // 设置表头

        Dimension size = table.getTableHeader().getPreferredSize();
        size.height = 40;//设置新的表头高度32
        table.getTableHeader().setPreferredSize(size);
        table.getTableHeader().setFont(new Font(null, Font.BOLD, 15));// 设置表头名称字体样式
        table.getTableHeader().setForeground(Color.white);// 设置表头名称字体颜色
        table.getTableHeader().setResizingAllowed(false);// 设置不允许手动改变列宽
        table.getTableHeader().setReorderingAllowed(false);// 设置不允许拖动重新排序各列
        table.getTableHeader().setBackground(new Color(47,53,66));
        ((DefaultTableCellRenderer)table.getTableHeader().getDefaultRenderer()).setHorizontalAlignment(JLabel.CENTER);//居中
        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.setPreferredSize(new Dimension(770,568));
        scrollPane.setBorder(BorderFactory.createLineBorder(Color.getHSBColor(0.0000F,0.0000F,0.9333F)));
        scrollPane.setVisible(true);
        table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
            public void valueChanged(ListSelectionEvent e)
            {
                if (e.getValueIsAdjusting()){
                    textField1.setText(table.getValueAt(table.getSelectedRow(), 0).toString());
                }
            }
        });
        return scrollPane;
    }
    public  JScrollPane viewCourseTable(List<Course> data){
        String[] name={"课程号","课程名称","课时","学分","教师","职称","人数","修改时间"};
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ;
        if (data == null){
            Object[][] tableData=new Object[0][8];
            return jScrollPane(tableData, name);
        }
        Object[][] tableData=new Object[data.size()][8];
        for (int i = 0; i < data.size(); i++) {
            tableData[i][0]= data.get(i).getCourseId();
            tableData[i][1]= data.get(i).getCourseName();
            tableData[i][2]= data.get(i).getCourseHour();
            tableData[i][3]= data.get(i).getCourseScore();
            //获取老师名称
            Long teacherId=data.get(i).getTeacherId();
            TeacherController teacherController=new TeacherController();
            Result<Teacher> teacherResult = teacherController.teacherInfo(teacherId);
            tableData[i][4]= teacherResult.getData().getTeacherName();
            tableData[i][5]= teacherResult.getData().getTeacherTitle();
            tableData[i][6]= data.get(i).getCourseCount();
            tableData[i][7]=  dateFormat.format(data.get(i).getUpdateTime());
        }
        return jScrollPane(tableData,name);
    }

    private void thisPropertyChange(PropertyChangeEvent e) {
        // TODO add your code here

    }

    private void thisMouseClicked(MouseEvent e) {
        // TODO add your code here

    }

    private void button2MouseClicked(MouseEvent e) {
        // TODO add your code here
        try {
            JFrame jFrame = new JFrame();
            addCourse addCourse = new addCourse();
            addCourse.setVisible(true);
            jFrame.setBounds(200,200,500,450);
            jFrame.add(addCourse);
            jFrame.setVisible(true);
            Container.getInstance().upDatePage();
            this.updateUI();
        }catch (NumberFormatException numberFormatException){
            JOptionPane.showMessageDialog(null,"请先选择课程");
        }
    }



    private void button3MouseClicked(MouseEvent e) {
        // TODO add your code here
        try {
            JFrame jFrame = new JFrame();
            upData data = new upData(Long.valueOf(textField1.getText()));
            data.setVisible(true);
            jFrame.setBounds(200,200,500,450);
            jFrame.add(data);
            jFrame.setVisible(true);
            Container.getInstance().upDatePage();
            this.updateUI();
        }catch (NumberFormatException numberFormatException){
            JOptionPane.showMessageDialog(null,"请先选择课程");
        }


    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        panel1 = new JPanel();
        label2 = new JLabel();
        textField1 = new JTextField();
        button1 = new Button(Button.CANCELBUTTON);
        button2 = new Button();
        button3 = new Button();

        //======== this ========
        addPropertyChangeListener(e -> thisPropertyChange(e));
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                thisMouseClicked(e);
            }
        });
        setLayout(new BorderLayout());

        //======== panel1 ========
        {
            panel1.setLayout(new FlowLayout(FlowLayout.CENTER, 26, 15));

            //---- label2 ----
            label2.setText("\u8bf7\u8f93\u5165\u8bfe\u7a0b\u53f7");
            label2.setIconTextGap(5);
            panel1.add(label2);

            //---- textField1 ----
            textField1.setColumns(7);
            panel1.add(textField1);

            //---- button1 ----
            button1.setText("\u5220\u9664\u8bfe\u7a0b");
            button1.addActionListener(e -> {
			button1ActionPerformed(e);
			button1ActionPerformed(e);
			button1ActionPerformed(e);
		});
            button1.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    button1MouseClicked(e);
                }
            });
            panel1.add(button1);

            //---- button2 ----
            button2.setText("\u65b0\u589e\u8bfe\u7a0b");
            button2.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    button2MouseClicked(e);
                }
            });
            panel1.add(button2);

            //---- button3 ----
            button3.setText("\u8bfe\u7a0b\u4fe1\u606f\u4fee\u6539");
            button3.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    button3MouseClicked(e);
                }
            });
            panel1.add(button3);
        }
        add(panel1, BorderLayout.NORTH);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel panel1;
    private JLabel label2;
    private JTextField textField1;
    private JButton button1;
    private JButton button2;
    private JButton button3;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
